/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ages.test.swat

import javax.media.opengl.GL
import com.ages.env.AbstractWorld
import com.ages.graphics.SpriteBufferManager
import com.ages.graphics.HudText
import com.ages.graphics.Sprite
import com.ages.math.Vector3D


class LoadingWorld extends AbstractWorld{
  var loadingDone = false
  var successor:AbstractWorld=_
  var stop = false
  val thingsToLoad = SpriteBufferManager.spriteQueueSize
  val fly:Fly = new Fly(Vector3D(300,300,0))
  var flyLoaded = false

  def setSuccessor(s:AbstractWorld)
  {
    this.successor = s
  }

  def think{
    if(!stop)
    {
      loadingDone = SpriteBufferManager.queueEmpty
//    println(loadingDone)
      if(loadingDone)
      {
        worldDone()
      }
    }
    fly.think
  }
  def draw(gl:GL){
    if(!flyLoaded)
      {
        flyLoaded=true
        SpriteBufferManager.forceBufferSprite(Fly.spr1,gl)
        SpriteBufferManager.forceBufferSprite(Fly.spr2,gl)
      }
    fly.draw(gl)
    gl.glClearColor(0.1f,0.1f,0.1f,0)
    val percent = (100.0-((SpriteBufferManager.spriteQueueSize.toDouble/thingsToLoad.toDouble)*100))
    HudText.draw(gl,"LOADING..."+percent.toInt+" % done",300,300)
    SpriteBufferManager.bufferNext(gl)
  }
  def worldDone()= {
    stop = true
//    println("worldDone")
    if(loadingDone)
    {
      for(wl <- worldListeners)
      {
//        println("worldFinished being called")
        wl.worldFinished(successor)
      }
    }
  }


}
